package l2hc.gameserver.network.clientpackets;

import l2hc.gameserver.Config;
import l2hc.gameserver.SecondaryPasswordController;
import l2hc.gameserver.network.serverpackets.Ex2ndPasswordAck;

/**
 * (ch)cS{S}
 * c: change pass?
 * S: current password
 * S: new password
 */
public class RequestEx2ndPasswordReq extends L2GameClientPacket
{
	private int _changePass;
	private String _password, _newPassword;
	
	@Override
	protected void readImpl()
	{
		_changePass = readC();
		_password = readS();
		if(_changePass == 2)
			_newPassword = readS();
	}
	
	@Override
	protected void runImpl()
	{
		if (!Config.SECOND_AUTH_ENABLED) {
			return;
		}
		SecondaryPasswordController spc = getClient().getSecondaryAuth();
		boolean exVal = false;
	
		if (_changePass == 0 && !spc.passwordExist())
			exVal = spc.savePassword(_password);
		else if (_changePass == 2 && spc.passwordExist())
			exVal = spc.changePassword(_password, _newPassword);
	
		if (exVal)
			getClient().sendPacket(new Ex2ndPasswordAck(Ex2ndPasswordAck.SUCCESS));
	}
}
